package pert;

import pert.Task;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @(#) fileParser.java
 */

public class fileParser
{
	private hw3Pert works;
	
	private Task createtasks;
	
	public List<Task> readFile( String fname )
	{
		String fileName;
		String line = null;
		//fileName="E:\\SE-2\\SY-Modeling\\Assignments\\"+fname;
		fileName=fname;
		List <Task> taskList=new ArrayList<Task>(); 
		

		try {
	        // FileReader reads text files in the default encoding.
	        FileReader fileReader = 
	            new FileReader(fileName);

	        // Always wrap FileReader in BufferedReader.
	        BufferedReader bufferedReader = 
	            new BufferedReader(fileReader);

	        while((line = bufferedReader.readLine()) != null) {
	        	//create new object
	        	Task tt =new Task();
	        	//split line into task id,duration and dependents
	        	StringTokenizer st2 = new StringTokenizer(line, ",");
	        	
	        	List <String> tList=new ArrayList<String>();
	        	
	        	while (st2.hasMoreElements()) {
	        		//read data into a list
	        		tList.add((String) st2.nextElement());
	        	
	        	}
	        	
	        	
	        	int l=tList.size();
	        	tt.setColor("black");
	        	tt.setId(tList.get(0));
	        	String du=tList.get(1);
	        	tt.setDuration(Integer.parseInt(du));
	        	for (int i=2;i<l;i++)
	        	{
	        		tt.addDependents(tList.get(i));
	        		
	        		
	        	}
	        	
	        	//System.out.println("First node"+tt.getId());
	        	//System.out.println("Duration of the first"+tt.getDuration());
	        	
	        	taskList.add(tt);
	        	
	        }	
	       // System.out.println("Tasks objects lengths  are"+taskList.size());
	       //************************************************************************************
	        int slst=0;
	        int x=taskList.size();
	        for (int p=0;p<x;p++){
	        	//System.out.println("Node"+taskList.get(p).getId());
	        	for(slst=0;slst<taskList.get(p).getDep().size();slst++){//
	        		for (int q=0;q<x;q++){
	        			if(taskList.get(p).getDep().get(slst).equals(taskList.get(q).getId())){//
	        				taskList.get(p).addNodes(taskList.get(q));
	        		
	        		}
	        		}
	        			
	        		}
	        		
	        	}
	       //close the file.
	        bufferedReader.close();			
	    }
	    catch(FileNotFoundException ex) {
	        System.out.println(
	            "Unable to open file '" + 
	            fileName + "'");				
	    }
	    catch(IOException ex) {
	        System.out.println(
	            "Error reading file '" 
	            + fileName + "'");					
	       
	    }
		return taskList;
		
	}
	
	
}

